package org.example.controller;

import org.example.TestApplication;
import org.hamcrest.Matchers;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

/**
 * @program: example-web-framework-extends-2x
 * @description:
 * @author: Chenjiabin
 * @create: 2018-05-14 18:54
 **/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = {TestApplication.class})
@AutoConfigureMockMvc
public class AjaxMethodControllerTester {
    @Autowired
    private ServerProperties properties;
    @Autowired
    private MockMvc mvc;

    @Test
    @WithMockUser(username = "user", roles = {"USER"})
    public void read() throws Exception {
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/ajax_method/read");
        builder.param("name", "name");
        builder.param("username", "username");
        ResultActions resultActions = this.mvc.perform(builder);

        resultActions.andExpect(MockMvcResultMatchers.status().isOk());
        resultActions.andExpect(MockMvcResultMatchers.jsonPath("$.success", Matchers.equalTo(true)));
        resultActions.andExpect(MockMvcResultMatchers.jsonPath("$.data.name", Matchers.equalTo("name")));
        resultActions.andExpect(MockMvcResultMatchers.jsonPath("$.data.username", Matchers.equalTo("username")));
    }

    @Test
    @WithMockUser(username = "user", roles = {"ADMIN"})
    public void save() throws Exception {
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/ajax_method/save");
        builder.param("name", "name");
        builder.param("username", "username");
        ResultActions resultActions = this.mvc.perform(builder);

        resultActions.andExpect(MockMvcResultMatchers.status().isOk());
        resultActions.andExpect(MockMvcResultMatchers.jsonPath("$.success", Matchers.equalTo(true)));
        resultActions.andExpect(MockMvcResultMatchers.jsonPath("$.data.name", Matchers.equalTo("name")));
        resultActions.andExpect(MockMvcResultMatchers.jsonPath("$.data.username", Matchers.equalTo("username")));
    }

    @Test
    @WithMockUser(username = "user", roles = {"USER"})
    public void save403() throws Exception {
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/ajax_method/save");
        builder.accept("application/json");
        builder.param("name", "name");
        builder.param("username", "username");
        ResultActions resultActions = this.mvc.perform(builder);
        resultActions.andExpect(MockMvcResultMatchers.status().isForbidden());

        resultActions.andExpect(MockMvcResultMatchers.jsonPath("$.success", Matchers.equalTo(false)));
        resultActions.andExpect(MockMvcResultMatchers.jsonPath("$.status.code", Matchers.equalTo(403)));
    }

    @Test
    public void save401() throws Exception {
        MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/ajax_method/save");
        builder.accept("application/json");
        builder.param("name", "name");
        builder.param("username", "username");
        ResultActions resultActions = this.mvc.perform(builder);
        String xxx = resultActions.andReturn().getResponse().getContentAsString();
        //打印信息
        resultActions.andDo(MockMvcResultHandlers.print());
        resultActions.andExpect(MockMvcResultMatchers.status().isUnauthorized());
        resultActions.andExpect(MockMvcResultMatchers.jsonPath("$.success", Matchers.equalTo(false)));
        resultActions.andExpect(MockMvcResultMatchers.jsonPath("$.status.code", Matchers.equalTo(401)));
    }
}
